//
//  GridViewCell.m
//  McRemote
//
//  Created by Yuncheng Shen on 12/11/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "GridViewCell.h"
#import "GridView.h"


#pragma mark - informal protocol interface : GridViewCellDelegate

@interface GridView (GridViewCellDelegate) 

- (void)deleteCell:(GridViewCell *)cell;
- (void)tapOnCell:(GridViewCell *)cell;
- (void)longPressOnCell:(GridViewCell *)cell;

@end


#pragma mark - GridViewCell implementation

@interface GridViewCell ()

- (void)addGestureRecognizers;
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer;
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer;
- (void)handleTapOnEditView:(UITapGestureRecognizer *)recognizer;

@end


@implementation GridViewCell

#pragma mark - accessor

@synthesize iconButton, titleLabel, editView;
@synthesize editingStyle = _editingStyle;
@synthesize gridView;

- (void)setEditingStyle:(GridViewCellEditingStyle)editingStyle
{
    if (editingStyle == _editingStyle) return;
    
    _editingStyle = editingStyle;
    
    if (editingStyle == GridViewCellEditingStyleDelete) {
        self.editView.hidden = NO;
        if (!self.editView.image) {
            self.editView.image = [UIImage imageNamed:@"cross.png"];
        }
    }
    else {
        self.editView.hidden = YES;
    }
}

#pragma mark - view life cycle

- (void)awakeFromNib
{    
    [self.iconButton setImage:[UIImage imageNamed:@"BlankButton.png"] forState:UIControlStateNormal];
    self.titleLabel.text = @"Title";
    self.titleLabel.textColor = [UIColor whiteColor];
    self.editingStyle = GridViewCellEditingStyleNone;
    self.editView.hidden = YES;
    
    [self addGestureRecognizers];
}

- (void)addGestureRecognizers
{
    UITapGestureRecognizer *singleTap = 
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self addGestureRecognizer:singleTap];
    singleTap.cancelsTouchesInView = NO;
    
    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                                                  action:@selector(handleLongPress:)];
    [self addGestureRecognizer:longPress];
    longPress.cancelsTouchesInView = NO;
    
    UITapGestureRecognizer *tapOnEditView =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnEditView:)];
    self.editView.userInteractionEnabled = YES;
    [self.editView addGestureRecognizer:tapOnEditView];
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    if (self.gridView && [self.gridView respondsToSelector:@selector(tapOnCell:)]) {
        [self.gridView tapOnCell:self];
    }
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if ([recognizer state] == UIGestureRecognizerStateBegan) {
        if (self.gridView && [self.gridView respondsToSelector:@selector(longPressOnCell:)]) {
            [self.gridView longPressOnCell:self];
        }
    }
}

- (void)handleTapOnEditView:(UITapGestureRecognizer *)recognizer
{
    if (self.editingStyle == GridViewCellEditingStyleDelete) {
        if (self.gridView && [self.gridView respondsToSelector:@selector(deleteCell:)]) {
            [self.gridView deleteCell:self];
        }
    }
}

#pragma mark - API

+ (GridViewCell *)emptyGridCell
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GridViewCell" owner:nil options:nil];
    GridViewCell *emptyCell = nil;
    
    for (id object in topLevelObjects) {
        if ([object isKindOfClass:[GridViewCell class]]) {
            emptyCell = (GridViewCell *)object;
            emptyCell.titleLabel.text = nil;
        }
    }
    
    return emptyCell;
}

@end


